/*****************************************************************************/

/*
 *      modulator.c  --  NEWQPSK modulator.
 *
 *      Copyright (C) 2000  Tomi Manninen, OH2BNS (tomi.manninen@hut.fi)
 *
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

/*****************************************************************************/

#define _GNU_SOURCE
#define _REENTRANT

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "modem.h"
#include "modemconfig.h"
#include "filter.h"
#include "newqpsktx.h"
#include "complex.h"
#include "tbl.h"

#include <stdlib.h>
#include <stdio.h>
#include <malloc.h>

/* --------------------------------------------------------------------- */

#define SAMPLERATE(x)	((float)(x)*SymbolLen/DataCarriers/SymbolBits)

static void *modconfig(struct modemchannel *chan, unsigned int *samplerate, const char *params[])
{
	struct txstate *s = calloc(1, sizeof(struct txstate));

	if ((s = calloc(1, sizeof(struct txstate))) == NULL)
		logprintf(MLOG_FATAL, "out of memory\n");
	s->chan = chan;
	s->saved = -1;
	if (params[0]) {
		s->bps = strtoul(params[0], NULL, 0);
		if (s->bps < 1000)
			s->bps = 1000;
		if (s->bps > 5000)
			s->bps = 5000;
	} else
		s->bps = 2500;
	if (params[3]) {
		s->tunelen = strtoul(params[3], NULL, 0);
		if (s->tunelen < 0)
			s->tunelen = 0;
		if (s->tunelen > 64)
			s->tunelen = 64;
	} else
		s->tunelen = 32;
	if (params[4]) {
		s->synclen = strtoul(params[4], NULL, 0);
		if (s->synclen < 16)
			s->synclen = 16;
		if (s->synclen > 64)
			s->synclen = 64;
	} else
		s->synclen = 32;
	*samplerate = (int) (3.0 * SAMPLERATE(s->bps) + 0.5);
	return s;
}

static void modinit(void *state, unsigned int samplerate)
{
	struct txstate *s = (struct txstate *)state;
	float rate, f1, f2;

	rate = samplerate / SAMPLERATE(s->bps);
	s->bufsize = rate * SymbolLen + 16;
	f1 = 0.1;
	f2 = 0.9;
	init_tbl();
	init_filter(&s->filt, rate, f1, f2);
	init_newqpsktx(state);

	s->samples = malloc(s->bufsize * sizeof(int16_t));
	s->cbuf = malloc(s->bufsize * sizeof(complex));
}

static int modmodulate(void *state, int16_t **pnt)
{
	struct txstate *s = (struct txstate *)state;
	int n = 0, i;

	if (!s->txdone)
	{
		n = newqpsktx(state, s->cbuf);
		for (i = 0; i < n; i++)
			s->samples[i] = (s->cbuf[i].re + s->cbuf[i].im) * 32768.0;
		*pnt = s->samples;
	}

	return n;
}

/* --------------------------------------------------------------------- */

struct modulator newqpskmodulator = {
        NULL,
        "newqpsk",
        modconfig,
        modinit,
        modmodulate,
        free
};

/* --------------------------------------------------------------------- */
